package com.bo.baby2.service.impl;

import com.bo.baby2.config.ReturnResult;
import com.bo.baby2.dao.TUserAccountMapper;
import com.bo.baby2.dao.TUserInfoMapper;
import com.bo.baby2.dao.TUserWalletMapper;
import com.bo.baby2.entity.TUserAccount;
import com.bo.baby2.entity.TUserInfo;
import com.bo.baby2.entity.TUserWallet;
import com.bo.baby2.service.UserInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @program: baby2_p2p
 * @description: userService实现层
 * @author:波
 * @create: 2020-07-13 12:06
 **/
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private TUserInfoMapper tUserInfoMapper;
    @Resource
    private TUserWalletMapper tUserWalletMapper;
    @Resource
    private TUserAccountMapper tUserAccountMapper;
    @Override
    public TUserInfo selectByPrimaryKey(String accountId) {
        return tUserInfoMapper.selectByPrimaryKey(accountId);
    }

    /**
     * 修改用户信息
     * @param record
     * @return
     */
    @Override
    public ReturnResult updateByPrimaryKeySelective(TUserInfo record) {
        ReturnResult returnResult=null;
        //查询是否存在信息
        TUserInfo tUserInfo = tUserInfoMapper.selectByPrimaryKey(record.getAccountId());
        if(tUserInfo!=null) {
            //完善用户信息
            int i = tUserInfoMapper.updateByPrimaryKeySelective(record);
            if(i>0){
                returnResult=ReturnResult.successResult("修改成功",null);
            }else {
                returnResult=ReturnResult.failResult("修改失败");
            }

        }else {
            //完善用户信息
            int i = tUserInfoMapper.insertSelective(record);
            if(i>0){
                TUserWallet tUserWallet = new TUserWallet();
                tUserWallet.setAccountId(record.getAccountId());
                tUserWallet.setCreditScore(100);
                Long score = 100 * 10000L;
                tUserWallet.setCreditLine(score);
                tUserWallet.setResidualCreditLine(score);
                //设置信用分
                int i1 = tUserWalletMapper.updateByPrimaryKeySelective(tUserWallet);
                TUserAccount tUserAccount=new TUserAccount();
                tUserAccount.setId(record.getAccountId());
                tUserAccount.setFillUserinfo(1);
                //修改UserAccount表的是否完善信息字段
                int i2 = tUserAccountMapper.updateByPrimaryKeySelective(tUserAccount);
                if (i1 > 0) {
                    returnResult=ReturnResult.successResult("新增成功,成功添加信用分",null);
                } else {
                    returnResult=ReturnResult.failResult("赠送信誉分失败");
                }
            }

        }
        return returnResult;
    }
}
